package com.sjme.util;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;

public class CommonUtil {
    public static <S, T> IPage<T> convertIPage(IPage<S> sourcePage, Class<T> targetClazz, Function<S, T> converter) {
        IPage<T> targetPage = new Page<>();
        targetPage.setCurrent(sourcePage.getCurrent());
        targetPage.setSize(sourcePage.getSize());
        targetPage.setTotal(sourcePage.getTotal());
        targetPage.setPages(sourcePage.getPages());

        List<S> sourceRecords = sourcePage.getRecords();
        List<T> targetRecords = sourceRecords.stream()
                .map(converter)
                .collect(Collectors.toList());

        targetPage.setRecords(targetRecords);
        return targetPage;
    }
}
